//
//  YSY_Game_MainViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_Game_MainViewController.h"
#import "YSY_Game_MainView.h"
#import "Masonry.h"
#import "YSY_Game_NewsViewController.h"
@interface YSY_Game_MainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UISegmentedControl *mainSegment;      //控制换页
@property (nonatomic, retain) UICollectionView *mainCollectionV;     //内容块
@property (nonatomic, assign) NSInteger currentPage;                //collection当前页
@end

@implementation YSY_Game_MainViewController

- (void)dealloc
{
    self.mainCollectionV.delegate = nil;
    self.mainCollectionV.dataSource = nil;
    [_mainCollectionV reloadData];
    [_mainSegment release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
}

- (void)createView {
    //添加 segment
    [self addSegmentControl];
    //添加 collectionView
    [self addCollectionView];
    
    
}

#pragma mark - 私有方法
/**
 *  添加SegmentControl
 */
- (void)addSegmentControl {
    
    NSArray *menuArr = [NSArray arrayWithObjects:@"新闻", @"娱乐", @"更多", nil];
    self.mainSegment = [[UISegmentedControl alloc] initWithItems:menuArr] ;
    self.mainSegment.frame = CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height / 15);
    [self.mainSegment addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    
    self.mainSegment.tintColor = [UIColor blackColor];
    [self.view addSubview:self.mainSegment];
    [self.mainSegment release];
 
}
//segement 方法
- (void) segmentAction {
    NSLog(@"%ld", self.mainSegment.selectedSegmentIndex);
    self.mainCollectionV.contentOffset = CGPointMake(self.view.bounds.size.width * self.mainSegment.selectedSegmentIndex, 0);
}
/**
 *  添加collection
 */
- (void)addCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - self.mainSegment.frame.origin.y  - self.mainSegment.bounds.size.height - 50);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    
    
    self.mainCollectionV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.mainCollectionV.pagingEnabled = YES;
    self.mainCollectionV.bounces = NO;
    self.mainCollectionV.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:self.mainCollectionV];
    [self.mainCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainSegment.mas_bottom).with.offset(1);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(- 49);
        make.left.right.equalTo(self.view);
    }];
    self.mainCollectionV.delegate = self;
    self.mainCollectionV.dataSource = self;
    [self.mainCollectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.mainCollectionV release];
}

#pragma mark - UICollectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mainSegment.numberOfSegments;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    switch (indexPath.item) {
        case 0:
        {
            YSY_Game_NewsViewController *newsVC = [[YSY_Game_NewsViewController alloc] init];
            newsVC.view.frame = cell.contentView.bounds;
            [cell.contentView addSubview:newsVC.view];
        }
            break;
        case 1:
            cell.contentView.backgroundColor = [UIColor redColor];
            break;
        case 2:
            cell.contentView.backgroundColor = [UIColor yellowColor];
            break;
        default:
            cell.contentView.backgroundColor = [UIColor orangeColor];
            break;
    }
    return  cell;
}

//滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger number = scrollView.contentOffset.x / self.view.bounds.size.width;
    
    self.mainSegment.selectedSegmentIndex = number;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
